<template>
  <div class="addEassay">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="作者" prop="worker">
        <el-input v-model="form.worker"></el-input>
      </el-form-item>
      <el-form-item label="图片" prop="img">
        <el-upload
          class="avatar-uploader"
          :action="`http://47.94.203.104:9000/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.img" :src="form.img" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="内容" prop="count">
        <div style="border: 1px solid #ccc">
          <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editor"
            :defaultConfig="toolbarConfig"
            :mode="mode"
          />
          <Editor
            style="height: 300px; overflow-y: hidden"
            v-model="html"
            :defaultConfig="editorConfig"
            :mode="mode"
            @onCreated="onCreated"
          />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="Add">立即新增</el-button>
        <el-button @click="$refs.form.resetFields()">清空</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import httpApi from "@/http";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
export default {
  components: { Editor, Toolbar },
  data() {
    return {
      editor: null,
      html: "<p>hello</p>",
      toolbarConfig: {},
      editorConfig: {
      placeholder: "请输入内容...",

        MENU_CONF: {
          uploadImage: {
            fieldName: "file",
            server: "http://47.94.203.104:9000/upload",
            customInsert(res, insertFn) {
              // res 即服务端的返回结果
              // 从 res 中找到 url alt href ，然后插入图片
              //   console.log(res);
              let url = res.data;
              insertFn(url);
            },
          },
        },
      },

      mode: "simple", //简易模式。仅支持部分功能
      form: {
        title: "",
        worker: "",
        img: "",
        count: "",
      },
      rules: {
        title: [{ required: true, message: "此字段必填", tigger: "blur" }],
        img: [{ required: true, message: "此字段必填", tigger: "blur" }],
        count: [{ required: true, message: "内容必填", tigger: "blur" }],
        worker: [{ required: true, message: "此字段必填", tigger: "blur" }],
      },
    };
  },
  methods: {
    Add() {
      this.form.count = this.editor.getHtml();
      this.$refs.form.validate((valid) => {
        if (valid) {
          let params = this.form;
          httpApi.eassay.AddEassay(params).then((res) => {
            // console.log(res);
            this.$message({
              message: '新增成功',
              type: 'success'
            });
            this.editor.setHtml(this.form.count)
            this.$refs.form.resetFields();
          });
        } else {
          this.$message.error("新增失败，请稍后重试");
        }
      });
    },
    onCreated(editor) {
      this.editor = Object.seal(editor);
    },
    /** 上传成功后执行 */
    handleAvatarSuccess(res, file) {
      // console.log(res);
      // 返回图片地址
      this.form.img = res.data;
    },
    /** 上传文件的格式 / 大小验证 */
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style lang="scss" scoped>
.avatar-uploader {
  ::v-deep .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  display: block;
}
.addEassay{
    height: 620px;
    overflow: scroll;
}
</style>
